package com.bo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bo.bean.UserInformation;
import com.bo.dao.UserMapper;
import com.bo.datasource.DynamicDataSourceContextHolder;
@Service
public class UserServiceImp implements UserService {
	@Autowired
	UserMapper userMapper;
	/**
	 * 1、通过AOP获取注解的数据源类型并保存到ThreadLocal中（主要实现类：DynamicDataSourceAspect）
	 * 2、Spring就可以通过ThreadLocal获取当前线程的数据源类型进行切换（主要实现类：DynamicDataSource）
	 *  原文链接：https://blog.csdn.net/u013515384/article/details/89056190
	 *  	@DataSourceAnnotation(DataSourceType.miya) // 注解aop实现切换数据库
	 * */
	@Override
	//	@DataSourceAnnotation(DataSourceType.miya) // 注解aop实现切换数据库
	public UserInformation select(String user_id) {
		// TODO Auto-generated method stub
		//		DynamicDataSourceContextHolder.clearDataSource();
		//		DynamicDataSourceContextHolder.setDataSource(DataSourceType.miya_shop);
//		System.out.println(DynamicDataSourceContextHolder.getDataSource());
		try {
			UserInformation user=userMapper.getByUserId(user_id);
			return user;
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("出错啦");
		}
		return null;
	}
	@Override
	public UserInformation select1(String user_id) {
		// TODO Auto-generated method stub
		UserInformation one=new UserInformation();
		one.setUser_id(user_id);
		System.out.println(user_id);
		String table_name="lafeng_shop_taobao_user_account_id_"+user_id.substring(user_id.length()-2,user_id.length());
		System.out.println(table_name);
		one=userMapper.getByUserId1(one,table_name);
		return one;
	}

}
